﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using DB;

namespace Restarty_SIPO3.Class
{
    class SpracovanieSuboru
    {
        private FileInfo subor;
        public Error chyba = new Error();
        public Subor DataSubor = new Subor();
        public SpracovanieSuboru(FileInfo subory)
        {
            this.subor = subory;
        }


        internal Boolean AnalyzaSuboru(String Stredisko)
        {
            chyba = new Error();
            
            String tmpStr = null;
            Boolean citaj = true;
            String Kod = null;
            try
            {
                TextReader tr = new StreamReader(subor.FullName, Encoding.GetEncoding("windows-1250"));
                do
                {
                    if (citaj)
                        tmpStr = tr.ReadLine();
                    citaj = true;
                    if (tmpStr != null)
                    {
                        if (tmpStr.IndexOf((char)0x09) > 0)
                            Kod = tmpStr.Substring(0, tmpStr.IndexOf((char)0x09));
                        else
                            Kod = tmpStr;
                        switch (Kod)
                        {
                            case "1": DataSubor.vlozHlavicku(tmpStr, Stredisko);
                                break;
                            case "2": DataSubor.vlozZakaznika(tmpStr);
                                break;
                            case "3": DataSubor.vlozSluzbu(tmpStr);
                                break;
                            case "4": DataSubor.vlozRajon(tmpStr);
                                break;
                            case "5": DataSubor.vlozPostu(tmpStr);
                                break;
                            case "6": DataSubor.vlozPatu(tmpStr);
                                break;
                        }
                    }
                } while (tmpStr != null);

                tr.Close();
            }
            catch(Exception e)
            {
                chyba.Chyba = e;
            }
            return chyba.IsError;
        }


        internal bool PridaneDoDB()
        {
            Subor subor = new Subor(DataSubor);
            Posta posty = new Posta();
            
            prihlasenieNaDB databaza = new prihlasenieNaDB();
            String tmpStr = "CALL vlozSubor('" + this.subor.Name + "','";
            tmpStr += subor.CastPSC + "','" + subor.MesiacRok + "'," + subor.SP + ",'" + subor.HlavickaKod4 + "',";
            tmpStr += subor.PocetKlientov + ",'" + subor.Znamienko + "'," + subor.CelkovaSuma + ",'" + subor.Stredisko + "');";

            databaza.vykonajSQLfunkciu(tmpStr);
            if (!databaza.IsError)
            {
                tmpStr = "CALL vlozPosty('";
                tmpStr += posty.VytvorRetazec(DataSubor,this.subor.Name) + "');";
                databaza.vykonajSQLfunkciu(tmpStr);
            }
            if (!databaza.IsError)
            {
                Rajon rajony = new Rajon();
                tmpStr = "CALL vlozRajony('";
                tmpStr += rajony.VytvorRetazec(DataSubor,subor.SP) + "');";
                databaza.vykonajSQLfunkciu(tmpStr);
            }
            if (!databaza.IsError)
            {
                Zakaznik zakaznici = new Zakaznik();
                ArrayList RetazceNaVykonanie = zakaznici.VytvorRetazce(DataSubor);
                foreach (String ret in RetazceNaVykonanie)
                {
                    tmpStr = "CALL vlozZakaznici('";
                    tmpStr += ret + "');";
                    databaza.vykonajSQLfunkciu(tmpStr);
                }
            }

            if (!databaza.IsError)
            {
                Sluzba sluzby = new Sluzba();
                ArrayList RetazceNaVykonanie = sluzby.VytvorRetazce(DataSubor);
                foreach (String ret in RetazceNaVykonanie)
                {
                    tmpStr = "CALL vlozSluzby('";
                    tmpStr += ret + "');";
                    databaza.vykonajSQLfunkciu(tmpStr);
                }
            }

            if (databaza.IsError)
            {
                chyba.IsError = databaza.IsError;
                chyba.ErrorText = databaza.ErrorText;
            }
            return chyba.IsError;
        }
    }
}
